package com.hykj.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hykj.dao.BusinessesDao;
import com.hykj.toolsbean.StringHandler;
import com.hykj.valuebean.businesses;

/**
 * Servlet implementation class BusinessesServlet
 */
public class BusinessesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BusinessesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String servletpath = request.getServletPath();
		if ("/businesses/getAllBusinesses".equals(servletpath)) {
			getAllBusinesses(request, response);
		} else if ("/admin/businesses/insertBusiness".equals(servletpath)) {
			insertBusiness(request, response);
		} else if ("/businesses/viewBusiness".equals(servletpath)) {
			viewBusiness(request, response);
		} else if ("/admin/listBusinesses".equals(servletpath)) {
			listBusinesses(request, response);
		} else if ("/admin/businesses/viewBusiness".equals(servletpath)) {
			adminViewBusiness(request, response);
		} else if ("/admin/businesses/deleteBusiness".equals(servletpath)) {
			deleteBusiness(request, response);
		} else if ("/businesses/getCategoryBusinesses".equals(servletpath)) {
			getCategoryBusinesses(request, response);
		} else if ("/admin/businesses/updateBusiness".equals(servletpath)) {
			updateBusiness(request, response);
		} else if ("/admin/businesses/getCategoryBusinesses".equals(servletpath)) {
			adminGetCategoryBusinesses(request, response);
		}
	}
	
	protected void adminGetCategoryBusinesses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			BusinessesDao BusinessesDaoIns = new BusinessesDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String category = request.getParameter("category");
			String goWhich = "/hykj/businesses/getCategoryBusinesses?category=" + category;
			List<?> NewsList = BusinessesDaoIns.getCategoryBusinesses(strcurrentP, strcurrentG,
					goWhich, category);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", BusinessesDaoIns.getDaoPage());
			BusinessesDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/admin/businesses/listBusinesses.jsp");
		rd.forward(request, response);
	}

	protected void getAllBusinesses(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			BusinessesDao BusinessesDaoIns = new BusinessesDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String goWhich = "/hykj/businesses/getAllBusinesses";
			List<?> NewsList = BusinessesDaoIns.getAllBusinesses(strcurrentP, strcurrentG,
					goWhich);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", BusinessesDaoIns.getDaoPage());
			BusinessesDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/businesses/businesses.jsp");
		rd.forward(request, response);
	}
	
	protected void updateBusiness(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BusinessesDao BusinessesDaoIns = new BusinessesDao();
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		Date date = new Date();
		
		businesses businessIns = new businesses();
		businessIns.setArticle(request.getParameter("article"));
		businessIns.setAuthor(request.getParameter("author"));
		businessIns.setDate(dateformat.format(date));
		businessIns.setOriginal(request.getParameter("original"));
		businessIns.setTitle(request.getParameter("title"));
		businessIns.setCategory(request.getParameter("category"));
		businessIns.setId(StringHandler.strToint(request.getParameter("id")));
		
		BusinessesDaoIns.updateBusinesses(businessIns);
		BusinessesDaoIns.closed();
		
		RequestDispatcher rd = request.getRequestDispatcher("/admin/listBusinesses");
		rd.forward(request, response);
	}

	protected void insertBusiness(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		BusinessesDao BusinessesDaoIns = new BusinessesDao();
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		Date date = new Date();
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String original = request.getParameter("original");
		String article = request.getParameter("article");
		String category = request.getParameter("category");

		businesses businessIns = new businesses();
		businessIns.setArticle(article);
		businessIns.setAuthor(author);
		businessIns.setDate(dateformat.format(date));
		businessIns.setOriginal(original);
		businessIns.setTitle(title);
		businessIns.setCategory(category);

		BusinessesDaoIns.insertBusinesses(businessIns);
		BusinessesDaoIns.closed();

		RequestDispatcher rd = request.getRequestDispatcher("/admin/listBusinesses");
		rd.forward(request, response);
	}

	protected void viewBusiness(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer newId = StringHandler.strToint(request.getParameter("newId"));
		if (newId != null) {

			BusinessesDao BusinessesDaoIns = new BusinessesDao();
			businesses businessIns = BusinessesDaoIns.getSingleBusiness(newId);
			BusinessesDaoIns.closed();

			if (businessIns != null) {
				request.setAttribute("newsIns", businessIns);
			}

		}
		RequestDispatcher rd = request.getRequestDispatcher("/businesses/viewBusiness.jsp");
		rd.forward(request, response);
	}

	protected void listBusinesses(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			BusinessesDao businessDaoIns = new BusinessesDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String goWhich = "/hykj/admin/listBusinesses";
			List<?> NewsList = businessDaoIns.getAllBusinesses(strcurrentP, strcurrentG,
					goWhich);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", businessDaoIns.getDaoPage());
			businessDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/admin/businesses/listBusinesses.jsp");
		rd.forward(request, response);
	}

	protected void adminViewBusiness(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer newId = StringHandler.strToint(request.getParameter("newId"));
		if (newId != null) {

			BusinessesDao businessDaoIns = new BusinessesDao();
			businesses businessesIns = businessDaoIns.getSingleBusiness(newId);
			businessDaoIns.closed();

			if (businessesIns != null) {
				request.setAttribute("newsIns", businessesIns);
			}

		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/admin/businesses/updateBusinesses.jsp");
		rd.forward(request, response);
	}

	protected void deleteBusiness(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String newId = request.getParameter("newId");
		BusinessesDao businessDaoIns = new BusinessesDao();
		businessDaoIns.deleteBusiness(newId);
		businessDaoIns.closed();
		
		RequestDispatcher rd = request.getRequestDispatcher("/admin/listBusinesses");
		rd.forward(request, response);

	}
	
	protected void getCategoryBusinesses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			BusinessesDao BusinessesDaoIns = new BusinessesDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String category = request.getParameter("category");
			String goWhich = "/hykj/businesses/getCategoryBusinesses?category=" + category;
			List<?> NewsList = BusinessesDaoIns.getCategoryBusinesses(strcurrentP, strcurrentG,
					goWhich, category);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", BusinessesDaoIns.getDaoPage());
			BusinessesDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/businesses/businesses.jsp");
		rd.forward(request, response);
	}

}
